package com.core.website.service.impl;

import cn.easyes.core.biz.PageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import com.core.website.entity.Content;
import com.core.website.entity.EsContent;
import com.core.website.esmapper.EsContentMapper;
import com.core.website.mapper.ContentMapper;
import com.core.website.service.EsService;
import com.core.website.util.CopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class EsServiceImpl implements EsService {
    @Autowired
    private EsContentMapper esContentMapper;
    @Autowired
    private ContentMapper contentMapper;

    public Integer insert(Content content) {
        if("article".equals(content.getType())){
            if(StringUtils.isEmpty(content.getId())){
                return 0;
            }
            Content content1 = esContentMapper.selectById(content.getId());
            if(null==content1){
                return esContentMapper.insert(content);
            }else{
                return esContentMapper.updateById(content);
            }
        }else{
            return 0;
        }
//        return esContentMapper.insert(content);
    }
    public Integer updateById(Content content) {
        if(!"article".equals(content.getType())){
            return 0;
        }
        Content content1 = esContentMapper.selectById(content.getId());
        if(null==content1){
            return esContentMapper.insert(content);
        }else{
            return esContentMapper.updateById(content);
        }
    }
    public Integer deleteById(String id) {
        Content content1 = esContentMapper.selectById(id);
        if(null!=content1){
            return esContentMapper.deleteById(id);
        }else{
            return 1;
        }
    }
    public List<Content> statistics(String yearAndMonth) {
        LambdaEsQueryWrapper<Content> wrapper = new LambdaEsQueryWrapper<>();
        int year=Integer.parseInt(yearAndMonth.split("-")[0]);
        int month=Integer.parseInt(yearAndMonth.split("-")[1]);
        wrapper.ge(Content::getApprovalTime,getFirstDayOfMonth(year,month));
        wrapper.lt(Content::getApprovalTime,getLastDayOfMonth(year,month));
        wrapper.isNotNull(Content::getOrganization);
        return esContentMapper.selectList(wrapper);
    }
    public PageInfo<Content> search(Content content) {
        LambdaEsQueryWrapper<Content> wrapper = new LambdaEsQueryWrapper<>();
        if(StringUtils.isNotBlank(content.getApproval())){
            wrapper.eq(Content::getApproval, content.getApproval());
        }
        if(StringUtils.isNotBlank(content.getTitle())){
            wrapper.like(Content::getTitle, content.getTitle());
        }
        if(StringUtils.isNotBlank(content.getContent())){
            wrapper.like(Content::getContent, content.getContent());
        }
        if(StringUtils.isNotBlank(content.getColumnId())){
            wrapper.eq(Content::getColumnId, content.getColumnId());
        }
        if(StringUtils.isNotBlank(content.getOrganization())){
            wrapper.eq(Content::getOrganization, content.getOrganization());
        }
        if(StringUtils.isNotBlank(content.getAuthor())){
            wrapper.like(Content::getAuthor, content.getAuthor());
        }
        if(StringUtils.isNotBlank(content.getPostedBy())){
            wrapper.like(Content::getPostedBy, content.getPostedBy());
        }
        if(null!=content.getBeginTime()){
            wrapper.ge(Content::getCreateTime, content.getBeginTime());
        }
        if(null!=content.getEndTime()){
            wrapper.lt(Content::getCreateTime, content.getEndTime());
        }
        wrapper.orderByDesc(Content::getApprovalTime);
        return esContentMapper.pageQuery(wrapper,content.getPageNum(),content.getPageSize());
    }
    public PageInfo<Content> globalQueryAllContent(Content content) {
        LambdaEsQueryWrapper<Content> wrapper = new LambdaEsQueryWrapper<>();
        LambdaEsQueryWrapper<Content> or = wrapper.or();
        if(StringUtils.isNotBlank(content.getParam())){
            or.like(Content::getTitle, content.getParam());
            or.like(Content::getContent, content.getParam());
            or.like(Content::getPostedBy, content.getParam());
            or.like(Content::getAuthor, content.getParam());
        }
        wrapper.orderByDesc(Content::getApprovalTime);
        return esContentMapper.pageQuery(wrapper,content.getPageNum(),content.getPageSize());
    }
    @Override
    public void initContent(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                LambdaEsQueryWrapper<Content> wrapper = new LambdaEsQueryWrapper<>();
                esContentMapper.delete(wrapper);
                Content content=new Content();
                content.setApproval("1");
                content.setType("article");
                List<Content> contents = contentMapper.queryAll(content);
                if(null!=contents&&contents.size()>0){
                    esContentMapper.insertBatch(contents);
                }
            }
        }).start();

    }
    /**
     * 获取某年某月的第一天
     */
    public static Date getFirstDayOfMonth(int year, int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        return  cal.getTime();
    }
    /**
     * 获取某月的最后一天
     */
    public static Date getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        cal.set(Calendar.HOUR_OF_DAY,23);
        cal.set(Calendar.MINUTE,59);
        cal.set(Calendar.SECOND,59);
        return  cal.getTime();
    }


}
